package com.yc.spring.jdbc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@ComponentScan("com.yc.spring.jdbc")
// 开启事务管理
@EnableTransactionManagement
public class JdbcConfig {

    // 配置数据源
    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dmds = new DriverManagerDataSource();
        dmds.setDriverClassName("com.mysql.jdbc.Driver");
        dmds.setUrl("jdbc:mysql://127.0.0.1/C112-S3-Ply-bank");
        dmds.setUsername("root");
        dmds.setPassword("a");
        return dmds;
    }

    // JDBC模板对象 ==> DBHelper
    @Resource
    DataSource dataSource;
    @Bean
    JdbcTemplate jdbcTemplate(){
        return new JdbcTemplate(dataSource);
    }

    // 事务管理器
    @Bean
    DataSourceTransactionManager tm(){
        return new DataSourceTransactionManager(dataSource);
    }

}
